package edu.washington.cs.workflow.analysis.guistructure;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent;
import edu.washington.cs.workflow.util.Globals;
import edu.washington.cs.workflow.util.Utils;

public class EventSequence {
	
	List<UIEvent> eventList = new LinkedList<UIEvent>();
	
	Set<UIEvent> nextAvailableEvents = new LinkedHashSet<UIEvent>();
	
	//XXX bad design: should not contain an outcome field
	SequenceExecutionOutcome outcome = null;
	
	public void addUIEvent(UIEvent event) {
		Utils.checkNotNull(event);
		this.eventList.add(event);
	}
	
	public List<UIEvent> getEventList() {
		return this.eventList;
	}
	
	public void setOutcome(SequenceExecutionOutcome outcome) {
		Utils.checkNotNull(outcome);
		this.outcome = outcome;
	}
	
	/**
	 * Returns available events
	 * */
	public Collection<UIEvent> getAvailableActions() {
		return this.nextAvailableEvents;
	}
	
	public void addAvailableAction(UIEvent event) {
		Utils.checkNotNull(event);
		this.nextAvailableEvents.add(event);
	}
	
	/**
	 * Create a new event sequence
	 * */
	public EventSequence extend(UIEvent event) {
		EventSequence newSeq = new EventSequence();
		for(UIEvent e : this.eventList) {
			newSeq.addUIEvent(e);
		}
		newSeq.addUIEvent(event);
		return newSeq;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for(UIEvent event : eventList) {
			sb.append(event.getDescription());
			sb.append(Globals.lineSep);
		}
		if(this.outcome != null) {
		    sb.append(this.outcome.getOutcome());
		    sb.append(Globals.lineSep);
		}
		return sb.toString();
	}
}
